package cn.edu.swu;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;

public class connect {

    static String selectsql = null;
    static ResultSet retsult = null;

    public static final String url = "jdbc:mysql://localhost:3306/items";
    public static final String name = "com.mysql.jdbc.Driver";
    public static final String user = "root";
    public static final String password = "78954073";

    public static Connection conn = null;
    public static PreparedStatement pst = null;

    public static void main(String[] args) {
        int paraCount = 6; //读取参数数量
        selectsql = "select * from aitems";//SQL语句

        try {
            Class.forName(name);//指定连接类型
            conn = DriverManager.getConnection(url, user, password);//获取连接
            pst = conn.prepareStatement(selectsql);//准备执行语句
        } catch (Exception e) {
            e.printStackTrace();
        }

        String [] paras = new String [paraCount];
        try {
            retsult = pst.executeQuery();//执行语句，得到结果集

            while (retsult.next()) {
                for(int i = 0;i<paraCount;i++){
                    paras[i] = retsult.getString(i+1);
                }
                System.out.println(Arrays.toString(paras));

            }//显示数据
            retsult.close();
            conn.close();//关闭连接
            pst.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}